package rota.control;


import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import rota.dao.IContactDao;
import rota.model.Contact;
import rota.service.IContactService;

 
@Controller
public class ContactController {
 
    @Autowired
    private IContactService contactService;

    @Autowired
    private IContactDao contactDAO;
 
    
    @RequestMapping(value="/teste", method = RequestMethod.GET)
    public String listContacts(Map<String, Object> map) {
    	
    	//mapeia a chamada no jsp de uma lista e um contato
        map.put("contact", new Contact());
        map.put("contactList", contactService.listContact());
 
        return "contact";
    }
 
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @Transactional
    public String addContact(@ModelAttribute("contact")
    		Contact contact, BindingResult result) {
//        contactService.addContact(contact);
        contactDAO.addContact(contact);
 
        return "redirect:/teste.htm";
    }
 
    @RequestMapping("/delete/{contactId}")
    public String deleteContact(@PathVariable("contactId")
    Integer contactId) {
 
    	contactService.removeContact(contactId);
 
        return "redirect:/teste.htm";
    }
}

